package com.java.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.entity.Teacher;
import com.java.service.TeacherService;
import com.java.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class TeacherController extends BaseController{

    @Autowired
    private TeacherService teacherService;

    @PostMapping("teacher/insert")
    public ResultVo insert(@RequestBody Teacher teacher) {
        int rows = teacherService.insert(teacher);
        return toVo(rows);
    }

    @PostMapping("teacher/incrPaperCount")
    public ResultVo incrPaperCount(@RequestBody String teacherId) {
        if (!StringUtils.hasText(teacherId)){
            throw new RuntimeException("teacherId is null");
        }
        int rows = teacherService.incrPaperCount(Integer.valueOf(teacherId));
        return toVo(rows);
    }

    @PostMapping("teacher/delete")
    public ResultVo delete(@RequestBody String teacherId) {
        if (!StringUtils.hasText(teacherId)){
            throw new RuntimeException("teacherId is null");
        }
        int rows = teacherService.delete(Integer.valueOf(teacherId));
        return toVo(rows);
    }

    @PostMapping("teacher/update")
    public ResultVo update(@RequestBody Teacher teacher) {
        int rows = teacherService.update(teacher);
        return toVo(rows);
    }

    @PostMapping("teacher/selectOne")
    public ResultVo selectOne(@RequestBody String teacherId) {
        if (!StringUtils.hasText(teacherId)){
            throw new RuntimeException("teacherId is null");
        }
        Teacher teacher = teacherService.selectOne(Integer.valueOf(teacherId));
        return ResultVo.success(teacher);
    }

    @PostMapping("teacher/selectAll")
    public ResultVo selectAll() {
        List<Teacher> list = teacherService.selectAll();
        return ResultVo.success(list);
    }

    @PostMapping("teacher/selectByPage")
    public ResultVo selectByPage(@RequestBody Map<String, Object> map) {
        Integer pageNum = (Integer) map.remove("pageNum");
        Integer pageSize = (Integer) map.remove("pageSize");
        List<Teacher> list = teacherService.selectByPage(pageNum, pageSize, map);
        PageInfo<Teacher> pageInfo = new PageInfo<>(list);
        return ResultVo.success(pageInfo);
    }

}
